﻿using System;
using System.Data;
using System.Windows.Forms;
using HRM.HRMServiceProxy;

namespace HRM
{
    public partial class frmSalaryFundInfo : Form
    {
        bool isChange = false;
        SalaryFundInfo _SalaryFundInfo;
        SalaryFundClient _wcfSalaryFund;
        DialogResult dlgResult;
        int _SalaryFundID;
        int _month;
        int _year;
        PositionDetailClient _wcfPD;
        MainServiceClient wcfMainService;
        InterestRateClient _wcfInterest;
        //LeaveWorkClient _wcfFM;
        EmployeeClient _wcfEmployee;
        SalaryClient _wcfSalary;
        SalaryInfo _SalaryInfo;
        ParameterClient wcfParameter;
        decimal _rate;
        decimal _amount;
        decimal _SocialFM;
        decimal _MedicalFM;
        bool _SocialEmployee;
        bool _MedicalEmployee;
        decimal _IncreaseSalary;
        decimal _OldSalary;
        decimal _NewSalary;
        decimal _SalaryNewMember;
        decimal _LastBHYTThua;
        decimal _LastBHYTThieu;
        decimal _LastBHXHThua;
        decimal _LastBHXHThieu;
        decimal _BHYTThua;
        int _EmployeeID;
        DateTime _dateFM;
        int _IsApproved = 0;
        //decimal _IncreaseMedicalInc;
        //decimal _DecreaseMedicalInc;
        //decimal _IncreaseSocialInc;
        //decimal _DecreaseSocialInc;
        RoleRightInfo listRole;

        public delegate void AfterCloseBook();
        public event AfterCloseBook afterCloseBook;

        public frmSalaryFundInfo(int SalaryFundID, RoleRightInfo _listRole)
        {
            InitializeComponent();
            //LoadLanguage();
            dlgResult = DialogResult.Cancel;
            _SalaryFundInfo = new SalaryFundInfo();
            _wcfSalaryFund = Service.SalaryFund();
            _SalaryFundID = SalaryFundID;
            if (SalaryFundID > 0)
            {
                _SalaryFundInfo = _wcfSalaryFund.SalaryFund_GetInfo(SalaryFundID);
                _month = Convert.ToInt32(_SalaryFundInfo.Month.Month);
                _year = Convert.ToInt32(_SalaryFundInfo.Month.Year);
                _IsApproved = _SalaryFundInfo.IsApproved;
                if (_SalaryFundInfo.IsApproved == 2)//Đã đóng sổ
                {
                    btnCloseBook.Enabled = false;
                    btnSave.Enabled = false;
                }
                else
                {
                    btnCloseBook.Enabled = true;
                    btnSave.Enabled = true;
                }
                Info2Form();
            }
            listRole = _listRole;
            if (!listRole.Update)
            {
                btnCloseBook.Enabled = false;
                btnSave.Enabled = false;
            }

        }
        void LoadLanguage()
        {
            if (this.Tag != null)
                this.Text = UpdateUI.GetString(this.Tag.ToString());
            UpdateUI.Load_ToolStrip(ref toolStrip1);
            label1.Text = UpdateUI.GetString(label1.Tag.ToString());
            UpdateUI.Load_GroupBox(ref groupBox1);
            UpdateUI.Load_GroupBox(ref groupBox2);
        }
        void Info2Form()
        {
            txtMonth.Text = _SalaryFundInfo.Month.ToString("MM/yyyy");
            txtQuater.Text = _SalaryFundInfo.Quater;
            //Bảo hiểm Ytế:
            txtBHYT_Employee.Text = _SalaryFundInfo.BHYT_Employee.ToString("#,##0");
            txtBHYT_FundSalary.Text = _SalaryFundInfo.BHYT_Amount.ToString(UIParamater.Phay2So);
            txtBHYT_HavetoPaid.Text = _SalaryFundInfo.BHYT_HaveToPaid.ToString(UIParamater.Phay2So);
            txtBHYT_Decrease.Text = _SalaryFundInfo.BHYT_Decrease.ToString(UIParamater.Phay2So);
            txtBHYT_Increase.Text = _SalaryFundInfo.BHYT_Increase.ToString(UIParamater.Phay2So);
            txtBHYT_Sum.Text = _SalaryFundInfo.BHYT_Sum.ToString(UIParamater.Phay2So);
            txtBHYT_PaidAmount.Text = _SalaryFundInfo.BHYT_PaidAmount.ToString(UIParamater.Phay2So);
            txtBHYT_Thua.Text = _SalaryFundInfo.BHYT_Thua.ToString(UIParamater.Phay2So);
            txtBHYT_Thieu.Text = _SalaryFundInfo.BHYT_Thieu.ToString(UIParamater.Phay2So);
            txtBHYT_LastAmount_Thua.Text = _SalaryFundInfo.BHYT_KyTruocThua.ToString(UIParamater.Phay2So);
            txtBHYT_LastAmount_Thieu.Text = _SalaryFundInfo.BHYT_KyTruocThieu.ToString(UIParamater.Phay2So);

            //Bao hiem that nghiep:
            txtBHTN_Employee.Text = _SalaryFundInfo.BHTN_Employee.ToString("#,##0");
            txtBHTN_SalaryFund.Text = _SalaryFundInfo.BHTN_Amount.ToString(UIParamater.Phay2So);
            txtBHTN_HaveToPaid.Text = _SalaryFundInfo.BHTN_HaveToPaid.ToString(UIParamater.Phay2So);
            txtBHTN_Decrease.Text = _SalaryFundInfo.BHTN_Decrease.ToString(UIParamater.Phay2So);
            txtBHTN_InCrease.Text = _SalaryFundInfo.BHTN_Increase.ToString(UIParamater.Phay2So);
            txtBHTN_Sum.Text = _SalaryFundInfo.BHTN_Sum.ToString(UIParamater.Phay2So);
            txtBHTN_PaidAmount.Text = _SalaryFundInfo.BHTN_PaidAmount.ToString(UIParamater.Phay2So);
            txtBHTN_Thua.Text = _SalaryFundInfo.BHTN_Thua.ToString(UIParamater.Phay2So);
            txtBHTN_Thieu.Text = _SalaryFundInfo.BHTN_Thieu.ToString(UIParamater.Phay2So);
            txtBHTN_LastAmount_Thua.Text = _SalaryFundInfo.BHTN_KyTruocThua.ToString(UIParamater.Phay2So);
            txtBHTN_LastAmount_Thieu.Text = _SalaryFundInfo.BHTN_KyTruocThieu.ToString(UIParamater.Phay2So);

            //Bảo hiểm xã hội:
            txtBHXH_Employee.Text = _SalaryFundInfo.BHXH_Employee.ToString("#,##0");
            txtBHXH_FundSalary.Text = _SalaryFundInfo.BHXH_Amount.ToString(UIParamater.Phay2So);
            txtBHXH_HaveToPaid.Text = _SalaryFundInfo.BHXH_HaveToPaid.ToString(UIParamater.Phay2So);
            txtBHXH_Decrease.Text = _SalaryFundInfo.BHXH_Decrease.ToString(UIParamater.Phay2So);
            txtBHXH_Increase.Text = _SalaryFundInfo.BHXH_Increase.ToString(UIParamater.Phay2So);
            txtBHXH_Sum.Text = _SalaryFundInfo.BHXH_Sum.ToString(UIParamater.Phay2So);
            txtBHXH_PaidAmount.Text = _SalaryFundInfo.BHXH_PaidAmount.ToString(UIParamater.Phay2So);
            txtBHXH_Thua.Text = _SalaryFundInfo.BHXH_Thua.ToString(UIParamater.Phay2So);
            txtBHXH_Thieu.Text = _SalaryFundInfo.BHXH_Thieu.ToString(UIParamater.Phay2So);
            txtBHXH_LastAmount_Thua.Text = _SalaryFundInfo.BHXH_KyTruocThua.ToString(UIParamater.Phay2So);
            txtBHXH_LastAmount_Thieu.Text = _SalaryFundInfo.BHXH_KyTruocThieu.ToString(UIParamater.Phay2So);

            txtChamNop_KyTruoc.Text = _SalaryFundInfo.PhatKyTruoc.ToString(UIParamater.Phay2So);
            txtChamNop_KyNay.Text = _SalaryFundInfo.PhatKyNay.ToString(UIParamater.Phay2So);
            txtChamNop_DaDong.Text = _SalaryFundInfo.PhatDaNop.ToString(UIParamater.Phay2So);
            txtChamNop_KySau.Text = _SalaryFundInfo.PhatKySau.ToString(UIParamater.Phay2So);

            isChange = false;
            CalculateTotal();
        }

        void Form2Info()
        {
            _SalaryFundInfo = new SalaryFundInfo();
            _SalaryFundInfo.ID = _SalaryFundID;
            _SalaryFundInfo.BranchCode = Program.strBranchBHXH;
            _SalaryFundInfo.Month = UISetting.GetDate(txtMonth.Text);
            _SalaryFundInfo.Quater = txtQuater.Text;
            _SalaryFundInfo.IsApproved = _IsApproved;
            //Bao hiem y te:
            _SalaryFundInfo.BHYT_Employee = UISetting.GetIntNo(txtBHYT_Employee.Text);
            _SalaryFundInfo.BHYT_Amount = UISetting.GetDecimalNo(txtBHYT_FundSalary.Text);
            _SalaryFundInfo.BHYT_HaveToPaid = UISetting.GetDecimalNo(txtBHYT_HavetoPaid.Text);
            _SalaryFundInfo.BHYT_Increase = UISetting.GetDecimalNo(txtBHYT_Increase.Text);
            _SalaryFundInfo.BHYT_Decrease = UISetting.GetDecimalNo(txtBHYT_Decrease.Text);
            _SalaryFundInfo.BHYT_PaidAmount = UISetting.GetDecimalNo(txtBHYT_PaidAmount.Text);
            _SalaryFundInfo.BHYT_Sum = UISetting.GetDecimalNo(txtBHYT_Sum.Text);
            _SalaryFundInfo.BHYT_Thieu = UISetting.GetDecimalNo(txtBHYT_Thieu.Text);
            _SalaryFundInfo.BHYT_Thua = UISetting.GetDecimalNo(txtBHYT_Thua.Text);
            _SalaryFundInfo.BHYT_KyTruocThieu = UISetting.GetDecimalNo(txtBHYT_LastAmount_Thieu.Text);
            _SalaryFundInfo.BHYT_KyTruocThua = UISetting.GetDecimalNo(txtBHYT_LastAmount_Thua.Text);

            //Bao hiem that nghiep:
            _SalaryFundInfo.BHTN_Employee = UISetting.GetIntNo(txtBHTN_Employee.Text);
            _SalaryFundInfo.BHTN_Amount = UISetting.GetDecimalNo(txtBHTN_SalaryFund.Text);
            _SalaryFundInfo.BHTN_HaveToPaid = UISetting.GetDecimalNo(txtBHTN_HaveToPaid.Text);
            _SalaryFundInfo.BHTN_Increase = UISetting.GetDecimalNo(txtBHTN_InCrease.Text);
            _SalaryFundInfo.BHTN_Decrease = UISetting.GetDecimalNo(txtBHTN_Decrease.Text);
            _SalaryFundInfo.BHTN_PaidAmount = UISetting.GetDecimalNo(txtBHTN_PaidAmount.Text);
            _SalaryFundInfo.BHTN_Sum = UISetting.GetDecimalNo(txtBHTN_Sum.Text);
            _SalaryFundInfo.BHTN_Thieu = UISetting.GetDecimalNo(txtBHTN_Thieu.Text);
            _SalaryFundInfo.BHTN_Thua = UISetting.GetDecimalNo(txtBHTN_Thua.Text);
            _SalaryFundInfo.BHTN_KyTruocThieu = UISetting.GetDecimalNo(txtBHTN_LastAmount_Thieu.Text);
            _SalaryFundInfo.BHTN_KyTruocThua = UISetting.GetDecimalNo(txtBHTN_LastAmount_Thua.Text);

            //Bao hiem xa hoi:
            _SalaryFundInfo.BHXH_Employee = UISetting.GetIntNo(txtBHXH_Employee.Text);
            _SalaryFundInfo.BHXH_Amount = UISetting.GetDecimalNo(txtBHXH_FundSalary.Text);
            _SalaryFundInfo.BHXH_HaveToPaid = UISetting.GetDecimalNo(txtBHXH_HaveToPaid.Text);
            _SalaryFundInfo.BHXH_Increase = UISetting.GetDecimalNo(txtBHXH_Increase.Text);
            _SalaryFundInfo.BHXH_Decrease = UISetting.GetDecimalNo(txtBHXH_Decrease.Text);
            _SalaryFundInfo.BHXH_PaidAmount = UISetting.GetDecimalNo(txtBHXH_PaidAmount.Text);
            _SalaryFundInfo.BHXH_Sum = UISetting.GetDecimalNo(txtBHXH_Sum.Text);
            _SalaryFundInfo.BHXH_Thieu = UISetting.GetDecimalNo(txtBHXH_Thieu.Text);
            _SalaryFundInfo.BHXH_Thua = UISetting.GetDecimalNo(txtBHXH_Thua.Text);
            _SalaryFundInfo.BHXH_KyTruocThieu = UISetting.GetDecimalNo(txtBHXH_LastAmount_Thieu.Text);
            _SalaryFundInfo.BHXH_KyTruocThua = UISetting.GetDecimalNo(txtBHXH_LastAmount_Thua.Text);

            _SalaryFundInfo.PhatKyTruoc = UISetting.GetDecimalNo(txtChamNop_KyTruoc.Text);
            _SalaryFundInfo.PhatKyNay = UISetting.GetDecimalNo(txtChamNop_KyNay.Text);
            _SalaryFundInfo.PhatDaNop = UISetting.GetDecimalNo(txtChamNop_DaDong.Text);
            _SalaryFundInfo.PhatKySau = UISetting.GetDecimalNo(txtChamNop_KySau.Text);
        }

        bool CheckValid()//kiểm tra dữ liệu nhập
        {
            DateTime dateVal;
            if (!DateTime.TryParse(txtMonth.Text, out dateVal))
            {
                txtMonth.Focus();
                MessageBox.Show(UpdateUI.GetString("nhapsaingaythang"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

            return true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (CheckValid())
            {
                _wcfSalaryFund = Service.SalaryFund();
                Form2Info();
                if (_SalaryFundInfo.ID > 0)//update
                {
                    if (!listRole.Update)
                    {
                        MessageBox.Show(UpdateUI.GetString("khongcoquyencapnhat"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    if (_wcfSalaryFund.SalaryFund_Update(_SalaryFundInfo))
                    {
                        Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("capnhatthanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                        isChange = false;
                        dlgResult = DialogResult.OK;
                    }
                    else
                    {
                        MessageBox.Show(UpdateUI.GetString("capnhatkhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        isChange = true;
                    }
                }
                else//insert
                {
                    if (!listRole.Create)
                    {
                        MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    _SalaryFundInfo.ID = _wcfSalaryFund.SalaryFund_Insert(_SalaryFundInfo);
                    if (_SalaryFundInfo.ID > 0)
                    {
                        Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("capnhatthanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                        isChange = false;
                        dlgResult = DialogResult.OK;
                    }
                    else
                    {
                        MessageBox.Show(UpdateUI.GetString("capnhatkhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        isChange = true;
                    }
                }
            }
        }

        private void frmSalaryFundInfo_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isChange)
            {
                if (MessageBox.Show(UpdateUI.GetString("bandathaydoidulieu") + "\n" + UpdateUI.GetString("coluutruockhithoatkhong"), UIParamater.TitleProject, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    btnSave_Click(null, null);
                    if (isChange)
                        e.Cancel = true;
                    else
                        e.Cancel = false;
                }
            }
            this.DialogResult = dlgResult;
        }

        private void frmSalaryFundInfo_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    if (!e.Alt && !e.Control)
                    {
                        this.ProcessTabKey(!e.Shift);
                        e.Handled = true;
                    }
                    break;
                case Keys.Escape:
                    this.Close();
                    break;
                case Keys.F4:
                    btnCloseBook_Click(null, null);
                    break;
                case Keys.F3:
                    btnSave_Click(null, null);
                    break;
            }
        }

        private void btnCloseBook_Click(object sender, EventArgs e)
        {
            if (!listRole.Update)
            {
                MessageBox.Show(UpdateUI.GetString("khongcoquyencapnhat"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (MessageBox.Show("Bạn có chắc chắn đóng sổ tháng này không!", UIParamater.TitleProject, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                //Đóng sổ kỳ này
                string sql = "Update HRM_SalaryFund SET SalaryFund_IsApproved = 2 WHERE SalaryFund_ID = " + _SalaryFundID.ToString();
                wcfMainService = Service.MainService();
                wcfMainService.ExecuteQuery(sql);

                //Đóng sổ các kỳ nhỏ hơn
                sql = " Update HRM_SalaryFund SET SalaryFund_IsApproved = 2 "
                    + " WHERE SalaryFund_ID IN ( SELECT SalaryFund_ID FROM HRM_SalaryFund WHERE SalaryFund_Month <= (SELECT SalaryFund_Month FROM HRM_SalaryFund WHERE SalaryFund_ID = " + _SalaryFundID.ToString() + " ))"
                    + " AND SalaryFund_IsApproved = 1 "
                    + " AND SalaryFund_BranchCode = '" + Program.strBranchCode + "'";
                wcfMainService.ExecuteQuery(sql);
                dlgResult = DialogResult.OK;
                Program.taskbarNotifier.Show(UIParamater.TitleProject, "Đóng sổ thành công!", UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
            }
            //if (afterCloseBook != null)
            //    afterCloseBook();
        }

        private void DataChange(object sender, EventArgs e)
        {
            isChange = true;
        }

        private void txtBHYT_PaidAmount_Leave(object sender, EventArgs e)
        {
            if (txtBHYT_PaidAmount.Text.Length == 0)
                txtBHYT_PaidAmount.Text = "0";
            CalculateBHYT();
            CalculateTotal();
        }
        void CalculateBHYT()
        {
            decimal _BHYT_SalaryFund = 0;
            decimal _BHYT_HaveToPaid = 0;
            decimal _BHYT_PaidAmount = 0;
            decimal _BHYT_Increase = 0;
            decimal _BHYT_Decrease = 0;
            decimal _BHYT_Thua = 0;
            decimal _BHYT_Thieu = 0;
            decimal _BHYT_KyTruocThua = 0;
            decimal _BHYT_KyTruocThieu = 0;
            decimal _BHYT_SUM = 0;

            _BHYT_SalaryFund = UISetting.GetDecimalNo(txtBHYT_FundSalary.Text);
            _BHYT_HaveToPaid = UISetting.GetDecimalNo(txtBHYT_HavetoPaid.Text);
            _BHYT_Increase = UISetting.GetDecimalNo(txtBHYT_Increase.Text);
            _BHYT_Decrease = UISetting.GetDecimalNo(txtBHYT_Decrease.Text);
            _BHYT_PaidAmount = UISetting.GetDecimalNo(txtBHYT_PaidAmount.Text);
            _BHYT_KyTruocThua = UISetting.GetDecimalNo(txtBHYT_LastAmount_Thua.Text);
            _BHYT_KyTruocThieu = UISetting.GetDecimalNo(txtBHYT_LastAmount_Thieu.Text);
            _BHYT_SUM = _BHYT_HaveToPaid + _BHYT_Increase - _BHYT_Decrease + _BHYT_KyTruocThieu - _BHYT_KyTruocThua;
            if (_BHYT_PaidAmount - _BHYT_SUM < 0)
                _BHYT_Thieu = _BHYT_SUM - _BHYT_PaidAmount;
            else
                _BHYT_Thua = _BHYT_PaidAmount - _BHYT_SUM;

            txtBHYT_Thieu.Text = _BHYT_Thieu.ToString("#,##0");
            txtBHYT_Thua.Text = _BHYT_Thua.ToString("#,##0");
            txtBHYT_Sum.Text = _BHYT_SUM.ToString(UIParamater.Phay2So);
        }
        void CalculateBHTN()
        {
            decimal _BHTN_SalaryFund = 0;
            decimal _BHTN_HaveToPaid = 0;
            decimal _BHTN_PaidAmount = 0;
            decimal _BHTN_Increase = 0;
            decimal _BHTN_Decrease = 0;
            decimal _BHTN_Thua = 0;
            decimal _BHTN_Thieu = 0;
            decimal _BHTN_KyTruocThua = 0;
            decimal _BHTN_KyTruocThieu = 0;
            decimal _BHTN_SUM = 0;

            _BHTN_SalaryFund = UISetting.GetDecimalNo(txtBHTN_SalaryFund.Text);
            _BHTN_HaveToPaid = UISetting.GetDecimalNo(txtBHTN_HaveToPaid.Text);
            _BHTN_Increase = UISetting.GetDecimalNo(txtBHTN_InCrease.Text);
            _BHTN_Decrease = UISetting.GetDecimalNo(txtBHTN_Decrease.Text);
            _BHTN_PaidAmount = UISetting.GetDecimalNo(txtBHTN_PaidAmount.Text);
            _BHTN_KyTruocThua = UISetting.GetDecimalNo(txtBHTN_LastAmount_Thua.Text);
            _BHTN_KyTruocThieu = UISetting.GetDecimalNo(txtBHTN_LastAmount_Thieu.Text);
            _BHTN_SUM = _BHTN_HaveToPaid + _BHTN_Increase - _BHTN_Decrease + _BHTN_KyTruocThieu - _BHTN_KyTruocThua;

            if (_BHTN_PaidAmount - _BHTN_SUM < 0)
                _BHTN_Thieu = _BHTN_SUM - _BHTN_PaidAmount;
            else
                _BHTN_Thua = _BHTN_PaidAmount - _BHTN_SUM;

            txtBHTN_Thieu.Text = _BHTN_Thieu.ToString("#,##0");
            txtBHTN_Thua.Text = _BHTN_Thua.ToString("#,##0");
            txtBHTN_Sum.Text = _BHTN_SUM.ToString(UIParamater.Phay2So);
        }
        void CalculateBHXH()
        {
            decimal _BHXH_SalaryFund = 0;
            decimal _BHXH_HaveToPaid = 0;
            decimal _BHXH_PaidAmount = 0;
            decimal _BHXH_Increase = 0;
            decimal _BHXH_Decrease = 0;
            decimal _BHXH_Thua = 0;
            decimal _BHXH_Thieu = 0;
            decimal _BHXH_KyTruocThua = 0;
            decimal _BHXH_KyTruocThieu = 0;
            decimal _BHXH_SUM = 0;

            _BHXH_SalaryFund = UISetting.GetDecimalNo(txtBHYT_FundSalary.Text);
            _BHXH_HaveToPaid = UISetting.GetDecimalNo(txtBHXH_HaveToPaid.Text);
            _BHXH_Increase = UISetting.GetDecimalNo(txtBHXH_Increase.Text);
            _BHXH_Decrease = UISetting.GetDecimalNo(txtBHXH_Decrease.Text);
            _BHXH_PaidAmount = UISetting.GetDecimalNo(txtBHXH_PaidAmount.Text);
            _BHXH_KyTruocThua = UISetting.GetDecimalNo(txtBHXH_LastAmount_Thua.Text);
            _BHXH_KyTruocThieu = UISetting.GetDecimalNo(txtBHXH_LastAmount_Thieu.Text);
            _BHXH_SUM = _BHXH_HaveToPaid + _BHXH_Increase - _BHXH_Decrease + _BHXH_KyTruocThieu - _BHXH_KyTruocThua;

            if (_BHXH_PaidAmount - _BHXH_SUM < 0)
                _BHXH_Thieu = _BHXH_SUM - _BHXH_PaidAmount;
            else
                _BHXH_Thua = _BHXH_PaidAmount - _BHXH_SUM;

            txtBHXH_Thieu.Text = _BHXH_Thieu.ToString("#,##0");
            txtBHXH_Thua.Text = _BHXH_Thua.ToString("#,##0");
            txtBHXH_Sum.Text = _BHXH_SUM.ToString(UIParamater.Phay2So);
        }

        private void txtBHTN_PaidAmount_Leave(object sender, EventArgs e)
        {
            if (txtBHTN_PaidAmount.Text.Length == 0)
                txtBHTN_PaidAmount.Text = "0";
            CalculateBHTN();
            CalculateTotal();
        }

        private void txtBHXH_PaidAmount_Leave(object sender, EventArgs e)
        {
            if (txtBHXH_PaidAmount.Text.Length == 0)
                txtBHXH_PaidAmount.Text = "0";
            CalculateBHXH();
            CalculateTotal();
        }
        void CalculateTotal()
        {
            decimal _BHYT_SalaryFund = 0;
            decimal _BHYT_HaveToPaid = 0;
            decimal _BHYT_PaidAmount = 0;
            decimal _BHYT_Increase = 0;
            decimal _BHYT_Decrease = 0;
            decimal _BHYT_Thua = 0;
            decimal _BHYT_Thieu = 0;
            decimal _BHYT_KyTruocThua = 0;
            decimal _BHYT_KyTruocThieu = 0;
            decimal _BHYT_SUM = 0;

            decimal _BHTN_SalaryFund = 0;
            decimal _BHTN_HaveToPaid = 0;
            decimal _BHTN_PaidAmount = 0;
            decimal _BHTN_Increase = 0;
            decimal _BHTN_Decrease = 0;
            decimal _BHTN_Thua = 0;
            decimal _BHTN_Thieu = 0;
            decimal _BHTN_KyTruocThua = 0;
            decimal _BHTN_KyTruocThieu = 0;
            decimal _BHTN_SUM = 0;

            decimal _BHXH_SalaryFund = 0;
            decimal _BHXH_HaveToPaid = 0;
            decimal _BHXH_PaidAmount = 0;
            decimal _BHXH_Increase = 0;
            decimal _BHXH_Decrease = 0;
            decimal _BHXH_Thua = 0;
            decimal _BHXH_Thieu = 0;
            decimal _BHXH_KyTruocThua = 0;
            decimal _BHXH_KyTruocThieu = 0;
            decimal _BHXH_SUM = 0;

            decimal _PhatKyTruoc = 0;
            decimal _PhatKyNay = 0;
            decimal _PhatDaDong = 0;
            decimal _PhatKySau = 0;

            _BHYT_SalaryFund = UISetting.GetDecimalNo(txtBHYT_FundSalary.Text);
            _BHYT_HaveToPaid = UISetting.GetDecimalNo(txtBHYT_HavetoPaid.Text);
            _BHYT_PaidAmount = UISetting.GetDecimalNo(txtBHYT_PaidAmount.Text);
            _BHYT_Increase = UISetting.GetDecimalNo(txtBHYT_Increase.Text);
            _BHYT_Decrease = UISetting.GetDecimalNo(txtBHYT_Decrease.Text);
            _BHYT_Thua = UISetting.GetDecimalNo(txtBHYT_Thua.Text);
            _BHYT_Thieu = UISetting.GetDecimalNo(txtBHYT_Thieu.Text);
            _BHYT_KyTruocThua = UISetting.GetDecimalNo(txtBHYT_LastAmount_Thua.Text);
            _BHYT_KyTruocThieu = UISetting.GetDecimalNo(txtBHYT_LastAmount_Thieu.Text);
            _BHYT_SUM = UISetting.GetDecimalNo(txtBHYT_Sum.Text);

            _BHTN_SalaryFund = UISetting.GetDecimalNo(txtBHTN_SalaryFund.Text);
            _BHTN_HaveToPaid = UISetting.GetDecimalNo(txtBHTN_HaveToPaid.Text);
            _BHTN_PaidAmount = UISetting.GetDecimalNo(txtBHTN_PaidAmount.Text);
            _BHTN_Increase = UISetting.GetDecimalNo(txtBHTN_InCrease.Text);
            _BHTN_Decrease = UISetting.GetDecimalNo(txtBHTN_Decrease.Text);
            _BHTN_Thua = UISetting.GetDecimalNo(txtBHTN_Thua.Text);
            _BHTN_Thieu = UISetting.GetDecimalNo(txtBHTN_Thieu.Text);
            _BHTN_KyTruocThua = UISetting.GetDecimalNo(txtBHTN_LastAmount_Thua.Text);
            _BHTN_KyTruocThieu = UISetting.GetDecimalNo(txtBHTN_LastAmount_Thieu.Text);
            _BHTN_SUM = UISetting.GetDecimalNo(txtBHTN_Sum.Text);

            _BHXH_SalaryFund = UISetting.GetDecimalNo(txtBHXH_FundSalary.Text);
            _BHXH_HaveToPaid = UISetting.GetDecimalNo(txtBHXH_HaveToPaid.Text);
            _BHXH_PaidAmount = UISetting.GetDecimalNo(txtBHXH_PaidAmount.Text);
            _BHXH_Increase = UISetting.GetDecimalNo(txtBHXH_Increase.Text);
            _BHXH_Decrease = UISetting.GetDecimalNo(txtBHXH_Decrease.Text);
            _BHXH_Thua = UISetting.GetDecimalNo(txtBHXH_Thua.Text);
            _BHXH_Thieu = UISetting.GetDecimalNo(txtBHXH_Thieu.Text);
            _BHXH_KyTruocThua = UISetting.GetDecimalNo(txtBHXH_LastAmount_Thua.Text);
            _BHXH_KyTruocThieu = UISetting.GetDecimalNo(txtBHXH_LastAmount_Thieu.Text);
            _BHXH_SUM = UISetting.GetDecimalNo(txtBHXH_Sum.Text);

            _PhatKyTruoc = UISetting.GetDecimalNo(txtChamNop_KyTruoc.Text);
            _PhatKyNay = UISetting.GetDecimalNo(txtChamNop_KyNay.Text);
            _PhatDaDong = UISetting.GetDecimalNo(txtChamNop_DaDong.Text);
            _PhatKySau = UISetting.GetDecimalNo(txtChamNop_KySau.Text);

            txtTotal_Amount.Text = (_BHYT_SalaryFund + _BHTN_SalaryFund + _BHXH_SalaryFund).ToString(UIParamater.Phay2So);
            txtTotal_Havetopaid.Text = (_BHYT_HaveToPaid + _BHTN_HaveToPaid + _BHXH_HaveToPaid).ToString(UIParamater.Phay2So);
            txtTotal_Paid.Text = (_BHYT_PaidAmount + _BHTN_PaidAmount + _BHXH_PaidAmount).ToString(UIParamater.Phay2So);
            txtTotal_Increase.Text = (_BHYT_Increase + _BHTN_Increase + _BHXH_Increase).ToString(UIParamater.Phay2So);
            txtTotal_Descrease.Text = (_BHYT_Decrease + _BHTN_Decrease + _BHXH_Decrease).ToString(UIParamater.Phay2So);
            txtTotal_Thua.Text = (_BHYT_Thua + _BHTN_Thua + _BHXH_Thua).ToString(UIParamater.Phay2So);
            txtTotal_Thieu.Text = (_BHYT_Thieu + _BHTN_Thieu + _BHXH_Thieu).ToString(UIParamater.Phay2So);
            txtTotal_LastAmount_Thua.Text = (_BHYT_KyTruocThua + _BHTN_KyTruocThua + _BHXH_KyTruocThua).ToString(UIParamater.Phay2So);
            txtTotal_LastAmount_Thieu.Text = (_BHYT_KyTruocThieu + _BHTN_KyTruocThieu + _BHXH_KyTruocThieu).ToString(UIParamater.Phay2So);
            txtTotal_Sum.Text = (_BHYT_SUM + _BHTN_SUM + _BHXH_SUM).ToString(UIParamater.Phay2So);

        }

        private void txtBHYT_LastAmount_Thua_Leave(object sender, EventArgs e)
        {
            CalculateBHYT();
            CalculateTotal();
        }

        private void txtBHYT_LastAmount_Thieu_Leave(object sender, EventArgs e)
        {
            CalculateBHYT();
            CalculateTotal();
        }

        private void txtBHTN_LastAmount_Thua_Leave(object sender, EventArgs e)
        {
            CalculateBHTN();
            CalculateTotal();
        }

        private void txtBHTN_LastAmount_Thieu_Leave(object sender, EventArgs e)
        {
            CalculateBHTN();
            CalculateTotal();
        }

        private void txtBHXH_LastAmount_Thua_Leave(object sender, EventArgs e)
        {
            CalculateBHXH();
            CalculateTotal();
        }

        private void txtBHXH_LastAmount_Thieu_Leave(object sender, EventArgs e)
        {
            CalculateBHXH();
            CalculateTotal();
        }

        private void txtChamNop_KyTruoc_Leave(object sender, EventArgs e)
        {
            if (txtChamNop_KyTruoc.Text.Length == 0)
                txtChamNop_KyTruoc.Text = "0";
            CalChamNop();
        }

        private void txtChamNop_KyNay_Leave(object sender, EventArgs e)
        {
            if (txtChamNop_KyNay.Text.Length == 0)
                txtChamNop_KyNay.Text = "0";
            CalChamNop();
        }

        private void txtChamNop_DaDong_Leave(object sender, EventArgs e)
        {
            if (txtChamNop_KyNay.Text.Length == 0)
                txtChamNop_KyNay.Text = "0";
            CalChamNop();
        }
        void CalChamNop()
        {
            decimal _KyTruoc = 0;
            decimal _KyNay = 0;
            decimal _DaNop = 0;
            decimal _KySau = 0;

            _KyTruoc = UISetting.GetDecimalNo(txtChamNop_KyTruoc.Text);
            _KyNay = UISetting.GetDecimalNo(txtChamNop_KyNay.Text);
            _DaNop = UISetting.GetDecimalNo(txtChamNop_DaDong.Text);

            _KySau = (_KyTruoc + _KyNay) - _DaNop;

            txtChamNop_KySau.Text = _KySau.ToString("#,##0");
        }
    }
}
